package com.jiang.dao;

import com.jiang.entity.Cart;
import com.jiang.entity.Goods;
import com.jiang.entity.Type;
import com.jiang.entity.User;
import com.jiang.utlis.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jiang.dao
 * @Description:
 * @Date 2022/5/20 21:58
 */
public class userimpl implements userDao {
    private QueryRunner qr = new QueryRunner(DBUtil.getDateSource());

    @Override
    public User selectUser(String name) {
        String sql = "select * from  tb_user  where username =?";
        try {
            return qr.query(sql, new BeanHandler<User>(User.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int Adduser(User user) {
        String sql ="insert  into  tb_user values(null,?,?,?,?,?,?,?)";
        try {
            return qr.update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getGender(),user.getStatus(),user.getRole(),user.getCode());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查看商品分类
     * @return
     */
    @Override
    public List<Type> findAll() {
        String sql ="select *from tb_goods_type";
        try {
            return qr.query(sql, new BeanListHandler<Type>(Type.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //产看所有商品
    @Override
    public List<Goods> goodsAll() {
        String sql ="select tb_goods.* ,tb_goods_type.name typeName from  tb_goods left join tb_goods_type on tb_goods.tid = tb_goods_type.id ";
        try {
            return qr.query(sql,new BeanListHandler<Goods>(Goods.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加商品
     * @param goods
     * @return
     */
    @Override
    public int Addgoods(Goods goods) {
        String sql="insert into tb_goods value(null,?,?,?,?,?,?,?)";
        try {
           return qr.update(sql,goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据商品id查询详细信息
     * @param id
     * @return
     */
    @Override
    public Goods findGoodsID(Integer id) {
        String sql= "select * from tb_goods where id =?";
        try {
            Goods query = qr.query(sql, new BeanHandler<Goods>(Goods.class), id);
            return  query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id查找
     * @param uid 用户id
     * @param pid  商品id
     * @return
     */
    @Override
    public Cart findCidPid(Integer pid , Integer uid) {
        String sql ="select * from tb_cart where pid= ? and uid = ?";
        try {
            Cart cart = qr.query(sql, new BeanHandler<Cart>(Cart.class), pid, uid);
            return  cart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 向购物车添加数据
     * @param cart
     */
    @Override
    public void AddCartOne(Cart cart) {
        String sql ="insert into tb_cart (uid,pid,num,money) value (?,?,?,?)";
        try {
            qr.update(sql, cart.getUid(), cart.getPid(), cart.getNum(), cart.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改数据购物车数据
     * @param cart 数据库对象
     * @return
     */
    @Override
    public void updateCatr(Cart cart) {
        String sql ="update tb_cart set  num=? ,money=? where cid=?";
        try {
            qr.update(sql, cart.getNum(), cart.getMoney(), cart.getCid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据用户id查询 购物车中的数据
     * @param uid 用户ID
     * @return
     */
    @Override
    public List<Cart> findUidCart(Integer uid) {
        String sql =" select *from tb_cart where uid= ?";
        try {
           return qr.query(sql ,new BeanListHandler<Cart>(Cart.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据商品id查询物品
     * @param cid
     * @return
     */
    @Override
    public Cart insertCartOne(Integer cid) {
        String sql="select * from tb_cart where cid =?";
        try {
            Cart cart = qr.query(sql, new BeanHandler<Cart>(Cart.class), cid);
            return cart;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 根据商品id查询商品
     * @param id
     * @return
     */
    @Override
    public Goods insertGoodstOne(Integer id) {

        String sql ="select * from tb_goods where id =?";
        try {
            Goods query = qr.query(sql, new BeanHandler<Goods>(Goods.class), id);
            return  query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据购物车 id 删除相关的数据
     * @param cid
     */
    @Override
    public void DeleteCatr(Integer cid) {
        String sql ="delete from tb_cart where cid =?";
        try {
            int update = qr.update(sql, cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
